<?php
/**
 * Copyright (c) 2006, PORTALIER Julien
 * 
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 * 
 * @package      FeatherCMS
 * @subpackage   Components
 * @copyright    Copyright (c) 2006, Julien PORTALIER
 * @link         http://julien.portalier.com
 * @license      http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

class FormHelper extends XhtmlHelper
{
	// fields
	
	function field($name, $label=null, $options)
	{
		// @todo automatically define field's type from DB one.
		// ...
	}
	
	function input($name, $label, $options=null, $attr=null)
	{
		return $this->__field(
			parent::input(isset($options['type']) ? $options['type'] : 'text', $name, $attr),
			$name, $label, &$options
		);
	}
	
	function hidden($name, $value=null, $attr=null)
	{
		if (!empty($value))
			$attr['value'] = $value;
		return parent::input('hidden', $name, $attr);
	}
	
	function textarea($name, $label, $options=null, $attr=null)
	{
		return $this->__field(parent::textarea($name, $attr), $name, $label, &$options);
	}
	
	function checkbox($name, $label, $options=null, $attr=null)
	{
		$options['class'] = 'checkbox';
		$options['ltr']   = false;
		return $this->__field(parent::checkbox($name, $attr), $name, $label, &$options);
	}
	
	function select($name, $label, $entries, $options=null, $attr=null)
	{
		return $this->__field(parent::select($name, &$entries, $attr), $name, $label, $options);
	}
	
	function selectMany($name, $label, $entries, $options=null, $attr=null)
	{
		return $this->__field(parent::selectMany($name, &$entries, $attr), $name, $label, $options);
	}
	
	function radio($name, $label, $entries, $options=null, $in_between='<br />', $attr=null)
	{
		return $this->__field(parent::radio($name, &$entries, $in_between, $attr), $name, $label, &$options);
	}
	
	
	function submit($label, $name=null, $attr=null)
	{
		if ($name == 'ok')
			$attr['class'] = (isset($attr['class']) ? $attr['class'] : '').' submit default';
		return parent::submit($label, $name, $attr);
	}
	
	// error reporting
	
	function hasErrors()
	{
		if (!empty($this->validationErrors))
		{
			$content = '';
			foreach($this->validationErrors as $fields)
			{
				foreach($fields as $error) {
					$content .= $this->contentTag('li', $error);
				}
			}
			
			return $this->contentTag('div', $this->contentTag('p', '<strong>'._("Please correct the following errors before proceeding:").'</strong>').
				$this->contentTag('ul', $content), array('class' => 'error_report'));
		}
	}
	
	// internals
	
	protected function __field($formField, $name, $label, $options)
	{
		$required = (isset($options['required']) and $options['required']) ? 'required' : 'optional';
		$star     = (isset($options['required']) and $options['required']) ? '*' : null;
		$help     = isset($options['help']) ? $options['help']  : null;
		$class    = isset($options['class']) ? ' '.$options['class'] : null;
		
		ob_start();
		
		// field
		if (!isset($options['ltr']) or $options['ltr'])
		{
			echo $this->label($label, $name, $help).' ';
			echo $formField.' ';
		}
		else
		{
			echo $formField.' ';
			echo $this->label($label, $name, $help).' ';
		}
		
		// error?
		$error = $this->errorMessage($name);
		if (!empty($error))
		{
#			echo $this->contentTag('span', $error, array('class' => 'error')).' ';
			$class .= ' error';
		}
		return $this->contentTag('p', ob_get_clean(), array('class' => $required.$class));
	}
}
?>